package com.toman.Adpter;

import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.toman.Entity.Gridview_Entity;
import com.toman.MyApplication;
import com.toman.R;

import java.util.ArrayList;

/**
 * Created by  GridView_adpter on Administrator on 2017/11/3 on 16:13.
 */

public class GridView_adpter extends BaseAdapter {

    //上下文对象
    Context context;
    //
    ArrayList<Gridview_Entity> arrayList;

    LayoutInflater inflater;

    MyApplication myApplication;

    public GridView_adpter(Context context, ArrayList<Gridview_Entity> arrayList, MyApplication myApplication) {
        this.context = context;
        this.arrayList = arrayList;
        this.myApplication = myApplication;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return arrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        AddPackage addPackage;
        if (null == convertView) {
            addPackage = new AddPackage();
            convertView = inflater.inflate(R.layout.gridview_item, null, false);
            addPackage.img = (ImageView) convertView.findViewById(R.id.mImg);
            addPackage.txt = (TextView) convertView.findViewById(R.id.mTxt);

            convertView.setTag(addPackage);
        } else {
            addPackage = (AddPackage) convertView.getTag();
        }

        try {
            addPackage.img.setBackgroundResource(arrayList.get(position).getImg());
            addPackage.txt.setText(arrayList.get(position).getTxt());
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (myApplication.getSp().getString("token", "").equals("toman")) {
            if (arrayList.get(position).getTxt().equals("维修管理")) {
                addPackage.txt.setTextColor(context.getResources().getColor(R.color.grey_300));
                addPackage.img.setAlpha(0.3f);
            } else if (arrayList.get(position).getTxt().equals("故障管理")) {
                addPackage.txt.setTextColor(context.getResources().getColor(R.color.grey_300));
                addPackage.img.setAlpha(0.3f);
            } else if (arrayList.get(position).getTxt().equals("产量审核")) {
                addPackage.txt.setTextColor(context.getResources().getColor(R.color.grey_300));
                addPackage.img.setAlpha(0.3f);
            } else if (arrayList.get(position).getTxt().equals("物料管理")) {
                addPackage.txt.setTextColor(context.getResources().getColor(R.color.grey_300));
                addPackage.img.setAlpha(0.3f);
            } else if (arrayList.get(position).getTxt().equals("更多")) {
                addPackage.txt.setTextColor(context.getResources().getColor(R.color.grey_300));
                addPackage.img.setAlpha(0.3f);
            }

        } else if (myApplication.getSp().getString("token", "").equals("operater")) {
            if (arrayList.get(position).getTxt().equals("远程启动")) {
                addPackage.txt.setTextColor(context.getResources().getColor(R.color.black));
            } else {
                addPackage.txt.setTextColor(context.getResources().getColor(R.color.grey_300));
                addPackage.img.setAlpha(0.3f);
            }

        } else if (myApplication.getSp().getString("token", "").equals("company")) {
            if (arrayList.get(position).getTxt().equals("物料管理")) {
                addPackage.txt.setTextColor(context.getResources().getColor(R.color.grey_300));
                addPackage.img.setAlpha(0.3f);
            } else if (arrayList.get(position).getTxt().equals("维修")) {
                addPackage.txt.setTextColor(context.getResources().getColor(R.color.grey_300));
                addPackage.img.setAlpha(0.3f);
            } else if (arrayList.get(position).getTxt().equals("产量审核")) {
                addPackage.txt.setTextColor(context.getResources().getColor(R.color.grey_300));
                addPackage.img.setAlpha(0.3f);
            } else if (arrayList.get(position).getTxt().equals("型号确认")) {
                addPackage.txt.setTextColor(context.getResources().getColor(R.color.grey_300));
                addPackage.img.setAlpha(0.3f);
            } else if (arrayList.get(position).getTxt().equals("更多")) {
                addPackage.txt.setTextColor(context.getResources().getColor(R.color.grey_300));
                addPackage.img.setAlpha(0.3f);
            }
//            else if (arrayList.get(position).getTxt().equals("批量预热")) {
//                addPackage.txt.setTextColor(context.getResources().getColor(R.color.grey_300));
//                addPackage.img.setAlpha(0.3f);
//            }
        }
        return convertView;
    }


    public class AddPackage {
        ImageView img;
        TextView txt;
    }


}
